<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="园区名称" prop="parkName">-->
<!--        <el-input-->
<!--          v-model="queryParams.parkName"-->
<!--          placeholder="请输入园区名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="园区颜色" prop="parkColor">-->
<!--        <el-input-->
<!--          v-model="queryParams.parkColor"-->
<!--          placeholder="请输入园区颜色"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="城市code" prop="cityCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.cityCode"-->
<!--          placeholder="请输入城市code"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:park:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:park:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:park:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:park:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="园区名称" align="center" prop="parkName" />
      <el-table-column label="园区颜色" align="center" prop="parkColor" >
        <template slot-scope="scope">
          <el-color-picker v-model="scope.row.parkColor" disabled></el-color-picker>
        </template>
      </el-table-column>
<!--      <el-table-column label="园区数据" align="center" prop="parkData" />-->
      <el-table-column label="城市名称" align="center" prop="cityName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpload(scope.row)"
            v-hasPermi="['system:park:edit']"
          >文件上传</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:park:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:park:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产业园区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <div class="mapBox" style="width: 100%;height: 70vh">
        <div class="left">
          <Map3 :id="form.id" :myForm="form"  v-if="open"  style="width: 100%;height: 100%"  ref="childMap"
                 @choiceCity="choiceCity"
                 :editAreaData="editAreaData"></Map3>
        </div>
        <div class="right">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="绘制区域" prop="parkData">
              <el-button type="primary" size="mini" @click="drawPolygon">绘制区域</el-button>
              <el-button type="primary" size="mini" @click="clearPolygon">重新绘制</el-button>
              <el-button type="primary" size="mini" v-if="!editPolygoning" @click="handleEditP">编辑区域</el-button>
              <el-button type="primary" size="mini" v-if="editPolygoning" @click="downEditPolygon">编辑完成</el-button>
            </el-form-item>
            <el-form-item label="园区名称" prop="parkName">
              <el-input v-model="form.parkName" placeholder="请输入园区名称" />
            </el-form-item>
            <el-form-item label="园区颜色" prop="parkColor">
              <el-color-picker v-model="form.parkColor" @change="changeColor"></el-color-picker>
            </el-form-item>
            <el-form-item label="边框颜色" prop="borderColor">
              <el-color-picker v-model="form.borderColor" @change="changeColor"></el-color-picker>
            </el-form-item>
            <el-form-item label="当前城市" prop="cityName">
              <el-input v-model="form.cityName" placeholder="请选择城市" disabled/>
            </el-form-item>
            <el-form-item label="园区描述" prop="parkProfile">
              <el-input v-model="form.parkProfile"
                        type="textarea"
                        :rows="4"
                        placeholder="请输入园区描述信息"/>
            </el-form-item>
            <el-form-item label="720云链接" prop="cloudLink">
              <el-input v-model="form.cloudLink"/>
              <span slot="label">
                  <el-tooltip content="输入的链接以`http(s)://`开头" placement="top">
                  <i class="el-icon-question"></i>
                  </el-tooltip>
                  720云链接
                </span>
            </el-form-item>
          </el-form>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改图片信息" :visible.sync="open2" width="80%" append-to-body>
      <el-form ref="form2"  label-width="80px">
        <el-form-item label="模型文件" prop="companyProfile">
          <fileUpload ref="fileUpload"
                      :uploadFiles="uploadFiles"
                      :isPark="true"
                      :handleUploadFile="handleUploadFile" :value="fileModal"></fileUpload>
        </el-form-item>
        <el-form-item label="描述大图" prop="fileImage">
          <img-upload ref="uploadImage1" :value="fileImage" :limit="1" :isDetails1="true"
                      :handleUploadImage="handleUploadImage" :uploadFiles="uploadFiles"
          ></img-upload>
        </el-form-item>
        <el-form-item label="轮播图" prop="fileImageList">
          <img-upload ref="uploadImage2" :value="fileImageList" :limit="10" :isDetails1="true"
                      :isList="true" :handleUploadImageList="handleUploadImageList" :uploadFiles="uploadFiles"
          ></img-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUpload">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPark, getPark, delPark, addPark, updatePark } from "@/api/system/park";
import Map3 from '../../../components/Map3'
import { mapGetters } from 'vuex'
import imgUpload from '@/components/ImageUpload/index.vue'
export default {
  name: "Park",
  components:{
    Map3,
    imgUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产业园区表格数据
      parkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 编辑图片层
      open2:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkName: null,
        parkColor: null,
        parkData: null,
        cityCode: null,
        cityName:null
      },
      // 表单参数
      form: {
        cityName:null
      },
      // 表单校验
      rules: {
        parkName: [
          { required: true, message: "园区名称不能为空", trigger: "blur" }
        ],
        parkColor: [
          { required: true, message: "园区颜色不能为空", trigger: "blur" }
        ],
        borderColor: [
          { required: true, message: "园区边框颜色不能为空", trigger: "blur" }
        ],
        parkData: [
          { required: true, message: "园区数据不能为空", trigger: "blur" }
        ],
        parkProfile: [
          { required: true, message: "描述信息不能为空", trigger: "blur" }
        ],
      },
      //区域 编辑中
      editPolygoning:false,
      //大图
      fileImage: '',
      //图片列表
      fileImageList: [],
      isFileImage: false,
      isFileModal:false,
      //模型
      fileModal:'',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产业园区列表 */
    getList() {
      this.loading = true;
      listPark(this.queryParams).then(response => {
        this.parkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.fileImage = null
      this.fileImageList = null
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parkName: null,
        parkColor: "#73D1FB",
        parkData: null,
        cityCode: null,
        createTime: null,
      };
      this.editPolygoning = false
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产业园区";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPark(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产业园区";
        this.$nextTick(()=>{
          this.$refs.childMap.cdCurrentCity(this.form.cityCode)
        })
        console.log(JSON.parse(this.form.parkData),"this.form.parkData")
      });
    },
    handleUpload(row){
      this.reset();
      const id = row.id || this.ids
      getPark(id).then(response => {
        this.fileImage = response.data.imageFile
        this.fileImageList = response.data.bannerFile
        this.fileModal = response.data.modelFile
        this.form = response.data
        this.open2 = true
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.editPolygoning){
            this.$refs.childMap.downEditPolygon()
          }
          if (this.form.id != null) {
            updatePark(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPark(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }

        }
      });
    },
    submitUpload(){
      //轮播图列表
      if (this.isPushFileList) {
        console.log('执行上传列表')
        this.$refs.uploadImage2.uploadData.fileType = 6
        this.$refs.uploadImage2.uploadData.parkId = this.form.id
        this.$refs.uploadImage2.handleSubmit()
      }
      //大图
      if (this.isFileImage) {
        console.log('执行上传大图')
        this.$refs.uploadImage1.uploadData.fileType = 5
        this.$refs.uploadImage1.uploadData.parkId = this.form.id
        this.$refs.uploadImage1.handleSubmit()
      }

      if(this.isFileModal){
        this.$refs.fileUpload.uploadData.fileType = 7
        this.$refs.fileUpload.uploadData.parkId = this.form.id
        this.$refs.fileUpload.handleSubmit()
      }

      if(!this.isFileImage && !this.isFileImage){
        this.open2 = false
      }

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产业园区编号为"' + ids + '"的数据项？').then(function() {
        return delPark(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/park/export', {
        ...this.queryParams
      }, `park_${new Date().getTime()}.xlsx`)
    },
    //开始绘制
    drawPolygon(){
      this.$refs.childMap.drawPolygon()
    },
    clearPolygon(){
      this.$refs.childMap.clearPolygon()
      this.$refs.childMap.reSetPolygon()
      this.$message.success("重置区域")
    },
    //编辑园区数据
    editAreaData(pathArray){
      this.form.parkData = pathArray
      console.log(pathArray,"pathArray")
    },
    //开始编辑 已绘制区域
    handleEditP(){
      if(this.form.parkData){
        this.$refs.childMap.editPolygon()
        this.editPolygoning = true
      }else{
        this.$message.error("请先绘制区域")
      }
    },
    //编辑完成
    downEditPolygon(){
      this.$refs.childMap.downEditPolygon()
      this.$message.success("编辑完成")
      this.editPolygoning = false
    },
    //选择城市
    choiceCity(code,name){
      this.$set(this.form,"cityName",name)
      this.$set(this.form,"cityCode",code)
    },
    //改变园区颜色
    changeColor(e){
      this.$refs.childMap.changePolygon()
    },
    handleUploadImage() {
      this.isFileImage = true
      console.log("改变了Banner")
    },
    handleUploadImageList() {
      this.isPushFileList = true
      console.log('改变了列表')
    },
    uploadFiles(){
      console.log("自组建触发")
      this.open2 = false
      this.$message.success("上传成功！")
    },
    handleUploadFile(){
      console.log("改变了Modal")
      this.isFileModal = true
    },
  },
  computed:{
    map3Form: {
      get() {
        return this.$store.state.map3.form
      },
      set(val) {
        this.$store.commit("SET_FORM",val)
      }
    },

  },

};
</script>

<style lang="scss" scoped>
.mapBox {
  display: flex;

  .left {
    flex: 2;
    height: 70vh;
    border: 2px solid #ccc;
  }

  .right {
    flex: 1;
    margin-left: 20px;
    height: 70vh;
  }
}
</style>
